package cn.semes.org.controller;

import cn.semes.basic.util.AjaxResult;
import cn.semes.basic.util.PageList;
import cn.semes.org.domain.Permission;
import cn.semes.org.query.DepartmentQuery;
import cn.semes.org.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")

public class PermissionController {

    @Autowired
    private IPermissionService service;


    @GetMapping()
    public List<Permission> findAll() {
        return service.findAll();
    }

    /**
     * 添加或者修改
     *
     * @param permission
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Permission permission) {
        if (null != permission.getId()) {
            // 修改
            service.update(permission);
        } else {
            // 添加
            service.add(permission);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        service.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        System.out.println(ids);
        service.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Permission> queryByPage(@RequestBody DepartmentQuery query) {
        return service.queryByPage(query);
    }


}
